Rem
'
' Copyright (c) 2010 Wiebo de Wit <wiebo.de.wit@gmail.com>.
'
' All rights reserved. Use of this code is allowed under the
' Artistic License 2.0 terms, as specified in the LICENSE file
' distributed with this code, or available from
' http://www.opensource.org/licenses/artistic-license-2.0.php
'
endrem

rem
bbdoc: Extension of the library image, adding editor specific functionality
endrem
Type TEditorImage Extends TParticleImage

	'image property group in editor
	Global group:TPropertyGroup

	'only used for drawing of the complete image + frame boxes in the editor
	Field _baseImage:TImage
	
	'node in the treeview this object is attached to.
	Field node:TGadget

	'item ids. passed along with events to identify modified items
	Const PROP_NAME:Int = 100
	Const PROP_DESCRIPTION:Int = 101
	Const PROP_RESOLUTION:Int = 102
	Const PROP_FILENAME:Int = 103
	Const PROP_FRAMESIZEX:Int = 104
	Const PROP_FRAMESIZEY:Int = 105
	Const PROP_FRAMECOUNT:Int = 106
	Const PROP_HANDLEPOINT:Int = 107
	
	
	
	Method New()
		_editorName = "New Image"
		_handlePoint = HANDLE_CENTER
	End Method
	
	
	
	rem
	bbdoc: Sets node associated with this image
	about: Called when the user creates a new image
	endrem
	Method SetNode(n:TGadget)
		node = n
	End Method
	
	
	
	rem
	bbdoc: Creates the items in the image group
	about: The PROP_IDs are passed along in the events generated by the items.
	The global group field is set to the property group.
	endrem
	Method SetupGroupItems(grp:TPropertyGroup)
		group = grp
		CreatePropertyItemString("Name", "", PROP_NAME, group)
		CreatePropertyItemString("Description", "", PROP_DESCRIPTION, group)
		CreatePropertyItemPath("File Name", "", PROP_FILENAME, group)
		Local choice:TPropertyitemchoice = CreatePropertyItemChoice("Handle", PROP_HANDLEPOINT, group)
		choice.AddItem("Center")
		choice.AddItem("Left")
		choice.AddItem("Right")
		choice.AddItem("Top")
		choice.AddItem("Bottom")
		CreatePropertyItemInt("Frame Size X", 0, PROP_FRAMESIZEX, group)
		CreatePropertyItemInt("Frame Size Y", 0, PROP_FRAMESIZEY, group)
		Local item1:TPropertyItemString = CreatePropertyItemString("Resolution", "", PROP_RESOLUTION, group)
		item1.SetReadOnly(True)
		Local item2:TPropertyItemInt = CreatePropertyItemInt("Frames", 0, PROP_FRAMECOUNT, group)
		item2.SetReadOnly(True)
		
	End Method
	

	
	rem
	bbdoc: Sets property group settings to those in the image
	about: Called when the user selects an image item in the editor
	endrem	
	Method SetPropertyGroupItems()
		group.SetStringByLabel("Name", _editorName)
		group.SetStringByLabel("Description", _description)
		group.SetPathByLabel("File Name", _imageFilename)
		group.SetStringByLabel("Resolution", GetImageWidth() + " x " + GetImageHeight())
		group.SetIntByLabel("Frames", _frameCount)
		group.SetIntByLabel("Frame Size X", _frameDimensionX)
		group.SetIntByLabel("Frame Size Y", _frameDimensionY)
		group.SetStringByLabel("File Name", _imageFilename)
		group.SetChoiceByLabel("Handle", _handlePoint)
	End Method
	
	
	
	rem
	bbdoc: Sets settings to property group values
	endrem
	Method ApplyPropertyGroup()
		_editorName = group.GetStringByLabel("Name")
		_description = group.GetStringByLabel("Description")
		_imageFilename = group.GetPathByLabel("File Name")
		_frameCount = group.GetIntByLabel("Frames")
		_frameDimensionX = group.GetIntByLabel("Frame Size X")
		_frameDimensionY = group.GetIntByLabel("Frame Size Y")
		_handlePoint = group.GetChoiceByLabel("Handle")
	End Method
	
	
	
	rem
	bbdoc: Changes image setting according to changed item
	about: changed item is in eventsource(), the label is retrieved and then
	the value is retrieved from the group
	endrem
	Method ChangeSetting(i:TPropertyItem)
		Local label:String = i.GetLabel()
		Select label
			Case "Name"
				_editorName = group.GetStringByLabel(label)
				SetGadgetText(node, _editorName)
			Case "Description"
				_description = group.GetStringByLabel(label)
			Case "File Name"
				ApplyImage( group.GetPathByLabel(label) )
			Case "Frame Size X"
				SetFrameDimensionX( group.GetIntByLabel(label))
				SetPropertyGroupItems()
			Case "Frame Size Y"
				SetFrameDimensionY( group.GetIntByLabel(label))
				SetPropertyGroupItems()
			Case "Handle"
				_handlePoint = group.GetChoiceByLabel(label)
				SetHandlePoint()
		End Select
	End Method

	
	
	
	Method GetImageWidth:Int()
		If _baseImage = Null Then Return 0
		Return _baseImage.width
	End Method

	
	
	Method GetImageHeight:Int()
		If _baseImage = Null Then Return 0
		Return _baseImage.height
	End Method	

		
	
	Method SetFrameDimensionX(value:Int)
		If _baseImage=Null Then _frameDimensionX=0;Return
		If value <= 0 Or value > _baseImage.width Then value = _baseImage.width
		_frameDimensionX = value
		DetermineFrameCount()
	End Method

	
	
	Method SetFrameDimensionY(value:Int)
		If _baseImage=Null Then _frameDimensionX=0;Return
		If value <= 0 Or value > _baseImage.height Then value = _baseImage.height
		_frameDimensionY = value
		DetermineFrameCount()
	End Method
	
	

	rem
	bbdoc: Tries to apply the _imagefileName value by loading the image
	about:
	endrem
	Method ApplyImage(newName:String)
		If newName = _imageFilename Then Return
		If _LoadImage(newName)
			_frameDimensionX = _baseImage.width
			_frameDimensionY = _baseImage.height
			_frameCount = 0
			_image = _baseImage
			_handlePoint = HANDLE_CENTER
			DetermineFrameCount()
			SetPropertyGroupItems()
		End If
	End Method
	
	
	
	rem
	bbdoc: loads image into editor type
	about: Uses _baseImage, not _image. Overrides TEditorImage._LoadImage()
	endrem
	Method _LoadImage:Int(name:String)
		If Not FileType(name) Then Return 0
		Local tempImage:TImage = LoadImage( name )
		If tempImage = Null Then Return 0
		
		_baseImage = tempImage
		_image = _baseImage
		_imageFilename=name
		SetImageHandle(_baseImage, _baseImage.width/2, _baseImage.height/2)
		Return 1
	End Method	
	
	
	
	rem
	bbdoc: determines the amount of frames in the loaded baseimage by
	looking at the entered frame dimensions
	endrem
	Method DetermineFrameCount()
		If _baseImage=Null Then Return
		Local x:Int = _baseImage.width / _frameDimensionX
		Local y:Int = _baseImage.height / _frameDimensionY
		_frameCount = x * y
		If _frameCount = 1 Then Return
		_LoadMultiFrameImage()				
	End Method

	
	
	rem
	bbdoc: Reloads the _baseImage as a multiframe image.
	keeps single frame _baseImage for drawing in the editor.
	endrem	
	Method _LoadMultiFrameImage()
		Local tempImage:TImage = LoadAnimImage( _imageFilename, _frameDimensionX, _frameDimensionY, 0, _frameCount )
		If tempImage = Null Then Return
		_image = tempImage
		Self.SetHandlePoint()
	End Method	
	
	
	
	rem
	bbdoc: Draws the image in the editor. it adds boxes to show the frames
	endrem
	Method DrawBaseImage(dx:Float, dy:Float)
		If _baseImage = Null Then Return
		SetBlend ALPHABLEND
		SetAlpha 0.25
		SetColor 0,0,0
		DrawRect dx-_baseImage.width/2, dy-_baseImage.height/2, _baseImage.width, _baseImage.height
		SetAlpha 1.0
		SetColor 255,255,255
		DrawImage _baseImage, dx,dy
		SetAlpha 0.25
		SetColor 0,255,100
		Local x:Int, y:Int
		While y < _baseImage.height
			While x < _baseImage.width
				drawRectangle(x+dx - _baseImage.width/2, y+dy - _baseImage.height/2, _frameDimensionX, _frameDimensionY)
				x:+ _frameDimensionX
			Wend
			x=0
			y:+ _frameDimensionY
		Wend
	End Method
	
	
	
	rem
	bbdoc: Returns a clone of this editor image
	about: id is not copied.
	endrem
	Method Clone:TEditorImage()
		Local i:TEditorImage = New TEditorImage		
		i._editorName = "Copy of " + _editorName
		i._description = _description
		i._imageFilename = _imageFilename
		i._frameDimensionX = _frameDimensionX
		i._frameDimensionY = _frameDimensionY
		i._image = _image
		i._baseImage = _baseImage
		i._frameCount = _frameCount
		i._handlePoint = _handlePoint
		Return i
	End Method
	
End Type

Function DrawRectangle(x:Float, y:Float, w:Float, h:Float )
	DrawLine x,y, x+w,y
	DrawLine x, y, x, y + h
	DrawLine x+w,y, x+w,y+h
	DrawLine x, y+h, x+w, y+h
End Function
